//JavaScript
var curentSiteSwapId;
var typeSiteSwap;

/**
 * MÚthodes d'envoi de requÛtes
 */ 

function getSiteSwap(idSiteSwap){
  if (idSiteSwap != -1){
    var url = 'include/bd/update_siteswap_bd.php';
    $.getJSON(url
        ,{type_requete:'get_siteswap'
          ,id_sswap:idSiteSwap}
        ,gestionReponseSiteSwap);
  }else{
    gestionReponseSiteSwap(null);  
  }
}


function validSiteSwap(){
// On valide l'siteswap en base
 var url = 'include/bd/update_siteswap_bd.php';
 var typeUpdate = 'add_siteswap';
 if (curentSiteSwapId != -1){
    typeUpdate = 'edit_siteswap';
 }
 var nbObject = $('#idNbObject')[0].value;
 var nom = $('#idNom')[0].value;
 var codeSSwap = $('#idCodeSSwap')[0].value;
 var codePhp = $('#idCodePhp')[0].value;
 var comments = $('#idComments')[0].value;
 var difficulte = $('#idDifficulte')[0].value;
 
 if ((trim(nbObject) == "")
    || (trim(nom) == "")
    || (trim(codeSSwap) == "")
    || (trim(codePhp) == "")
    ){
    if (trim(nbObject) == ""){
      alert("Vous devez mettre un titre.");
    }else if (trim(nom) == ""){
      alert("Vous devez mettre un nom.");
    }else if (trim(codeSSwap) == ""){
      alert("Vous devez mettre un code de SiteSwap.");
    }else if (trim(codePhp) == ""){
      alert("Vous devez mettre un code Ó envoyer au lecteur.");
    }
 }else{
   nom = htmlentities(nom);
   comments = htmlentities(comments);
   // Ó la suite de l'appel on mettra Ó jour les listes d'illustrations
   $.getJSON(url
      ,{type_requete:'update_siteswap'
        , type_update:typeUpdate
        , type_sswap:typeSiteSwap
        , id_siteswap:curentSiteSwapId
        , nb_object:nbObject
        , nom:nom
        , code_sswap:codeSSwap
        , code_php:codePhp
        , comments:comments
        , difficulte:difficulte}
      ,gestionReponseValidSiteSwap);
    
  }
}

/**
 * MÚthodes de gestion de la rÚcupÚration des requÛte
 */ 

function gestionReponseSiteSwap(siteswap)
{
    if (siteswap != null){
      $('#idNbObject')[0].value = siteswap.nbObject;
      $('#idNom')[0].value = html_entity_decode(siteswap.titre);
      $('#idCodeSSwap')[0].value = siteswap.codeSiteSwap;
      $('#idCodePhp')[0].value = siteswap.codeSiteSwapPhp;
      $('#idComments').html(html_entity_decode(siteswap.texte));
      $('#idDifficulte')[0].value = siteswap.difficulte;
    }
}

function gestionReponseValidSiteSwap(siteswap){
  
  changePage();
}


/**
 * MÚthodes d'Údition graphique
 */ 

function manageLayout(idSiteSwap, typeSiteSwapToSet){
  curentSiteSwapId = idSiteSwap;
  typeSiteSwap = typeSiteSwapToSet;
  var urlImg = "images/";
  if (idSiteSwap == -1){
    urlImg += "siteswap_"+typeSiteSwap+".png";
    $('#titreZoneSiteSwap').html("Ajouter un SiteSwap");
  }else{
    urlImg += "siteswap_"+typeSiteSwap+".png";
    $('#titreZoneSiteSwap').html("Editer un SiteSwap");
  }
  
  $('#idImgModule')[0].src = urlImg;
  $('#idLoadImgIllustration').hide();
}


function changePage(){
  document.location = 'index.php?page=siteswap&type_sswap='+typeSiteSwap; 
}
